/*
* @Author: Choisaaaa
* @Date: 2018/5/6  0:29
* React 路由Router
* 历史
* 跳转
* 事件
*/

// -----------------------1.页面路由：Router
window.location.href = 'http://www.baidu.com';
history.back();

// -----------------------2.哈希路由：Hash Router
window.location = '#hash';
window.onhashchange = function(){
    console.log('current hash:', window.location.hash);
}

// ------------------------3.h5Router

history.pushState('name', 'title', '/path');// 推进一个状态

history.replaceState('name', 'title', '/path');// 替换一个状态
// popstate
window.onpopstate = function(){
    console.log(window.location.href);
    console.log(window.location.pathname);
    console.log(window.location.hash);
    console.log(window.location.search);
}


// react-router@4.2.2
//BrowserRouter H5路由方式实现
//Route：路由规则
//Switch：路由选择
//Link：跳转导航
//Redirect：自动跳转
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom'


//定义一个子组件A
class A extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return (
            <div>
                这里是组件A
                <Switch>
                    <Route exact path={`${this.props.match.path}`} render={(route) => {
                    return <div>当前组件是不带参数的A</div>
                }}/>
                    <Route path={`${this.props.match.path}/sub`} render={(route) => {
                    return <div>当前组件是Sub</div>
                }}/>
                    <Route path={`${this.props.match.path}/:id`} render={(route) => {
                    return <div>当前组件是带参数的A, 参数是：{route.match.params.id}</div>
                }}/>
                </Switch>
            </div>
        )
    }
}

//定义一个子组件B
class B extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return <div>Component B</div>
    }
}

//定义一个容器示子组件Wrapper
class Wrapper extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return (
            <div>
                <Link to="/a">组件A</Link>
                <br/>
                <Link to="/a/123">带参数的组件A</Link>
                <br/>
                <Link to="/b">组件B</Link>
                <br/>
                <Link to="/a/sub">/a/sub</Link>
                {this.props.children}
            </div>
        );
    }
}

ReactDOM.render(
    <Router>
        <Wrapper>
            <Route path="/a" component={A}/>
            <Route path="/b" component={B}/>
        </Wrapper>
    </Router>,
    document.getElementById('app')
);